package com.wsc.wisdomchina.ai.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;

import com.wsc.wisdomchina.R;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 类说明
 * 
 * @author 沈与坤
 * @time 5:13:39
 */
public class SpannableStr {

	/** 图片数组们 */
	private static int[] pic = new int[]{
			R.mipmap.weixiao, R.mipmap.ciya, R.mipmap.hanxiao, R.mipmap.touxiao, R.mipmap.guzhang, R.mipmap.qiaoda,
			R.mipmap.yun, R.mipmap.lenghan, R.mipmap.jingya, R.mipmap.piezui, R.mipmap.ku, R.mipmap.yiwen, R.mipmap.fendou,
			R.mipmap.liulei, R.mipmap.kelian, R.mipmap.haixiu, R.mipmap.cahan, R.mipmap.kun, R.mipmap.shuai,
			R.mipmap.kongju, R.mipmap.fadai, R.mipmap.dabing, R.mipmap.zhutou, R.mipmap.liuhan, R.mipmap.zhuakuang,
			R.mipmap.yinxian, R.mipmap.se, R.mipmap.huaixiao, R.mipmap.aoman, R.mipmap.baiyan, R.mipmap.bishi,
			R.mipmap.zhouma, R.mipmap.fanu

	};
	private static String[] picName = new String[]{"/微笑", "/呲牙", "/憨笑", "/偷笑", "/鼓掌", "/敲打", "/晕", "/冷汗", "/惊讶", "/撇嘴",
			"/酷", "/疑问", "/奋斗", "/流泪", "/可怜", "/害羞", "/擦汗", "/困", "/衰", "/恐惧", "/发呆", "/大兵", "/猪头", "/流汗", "/抓狂",
			"/阴险","/色", "/坏笑", "/傲慢", "/白眼", "/鄙视", "/咒骂", "/发怒"};

	/**
	 * 格式化字符串，并返回Spannable 字体变大
	 *
	 * @param content
	 * @return
	 * @throws Exception
	 */
	public static SpannableStringBuilder fromStr2(String content) throws Exception {
		SpannableStringBuilder htmlContent = new SpannableStringBuilder(content);
		ArrayList<Integer> startArray = getPosArray2(content, "\\{");
		ArrayList<Integer> endArray = getPosArray2(content, "\\}");
		for (int i = 0; i < startArray.size(); i++) {
			htmlContent.replace(startArray.get(i) - 1, startArray.get(i), " ");
			htmlContent.replace(endArray.get(i), endArray.get(i) + 1, " ");
			htmlContent.setSpan(new AbsoluteSizeSpan(55), startArray.get(i), endArray.get(i),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return htmlContent;
	}

	/**
	 * 格式化字符串，并返回Spannable 字体变色 蓝色
	 *
	 * @param content
	 * @return
	 * @throws Exception
	 */
	public static SpannableStringBuilder colorStr2(String content) throws Exception {
		SpannableStringBuilder htmlContent = new SpannableStringBuilder(content);
		ArrayList<Integer> startArray = getPosArray2(content, "\\{");
		ArrayList<Integer> endArray = getPosArray2(content, "\\}");
		for (int i = 0; i < startArray.size(); i++) {
			htmlContent.replace(startArray.get(i) - 1, startArray.get(i), " ");
			htmlContent.replace(endArray.get(i), endArray.get(i) + 1, " ");
			htmlContent.setSpan(new ForegroundColorSpan(Color.parseColor("#33B5E5")), startArray.get(i),
					endArray.get(i), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return htmlContent;
	}

	/**
	 * 格式化字符串，并返回Spannable 字体变色 橘色
	 *
	 * @param content
	 * @return
	 * @throws Exception
	 */
	public static SpannableStringBuilder colorStr1(String content) throws Exception {
		SpannableStringBuilder htmlContent = new SpannableStringBuilder(content);
		ArrayList<Integer> startArray = getPosArray2(content, "\\{");
		ArrayList<Integer> endArray = getPosArray2(content, "\\}");
		for (int i = 0; i < startArray.size(); i++) {
			htmlContent.replace(startArray.get(i) - 1, startArray.get(i), " ");
			htmlContent.replace(endArray.get(i), endArray.get(i) + 1, " ");
			htmlContent.setSpan(new ForegroundColorSpan(Color.parseColor("#F08519")), startArray.get(i),
					endArray.get(i), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return htmlContent;
	}

	/**
	 * 格式化字符串，并返回Spannable 字体变色 橘色
	 *
	 * @param content
	 * @param color
	 * @return
	 * @throws Exception
	 */
	public static SpannableStringBuilder colorStr1(String content, int color) throws Exception {
		SpannableStringBuilder htmlContent = new SpannableStringBuilder(content);
		ArrayList<Integer> startArray = getPosArray2(content, "\\{");
		ArrayList<Integer> endArray = getPosArray2(content, "\\}");
		for (int i = 0; i < startArray.size(); i++) {
			htmlContent.replace(startArray.get(i) - 1, startArray.get(i), " ");
			htmlContent.replace(endArray.get(i), endArray.get(i) + 1, " ");
			htmlContent.setSpan(new ForegroundColorSpan(color), startArray.get(i), endArray.get(i),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return htmlContent;
	}

	/**
	 * 校验加重字,接口返回的"{}"改为"()",字体变色
	 *
	 * @param content
	 * @param color
	 *            字体颜色
	 * @return
	 * @throws Exception
	 */
	public static SpannableStringBuilder colorAdaptableStr(String content, int color) throws Exception {
		SpannableStringBuilder htmlContent = new SpannableStringBuilder(content);
		ArrayList<Integer> startArray = getPosArray2(content, "\\{");
		ArrayList<Integer> endArray = getPosArray2(content, "\\}");
		for (int i = 0; i < startArray.size(); i++) {
			htmlContent.replace(startArray.get(i) - 1, startArray.get(i), "(");
			htmlContent.replace(endArray.get(i), endArray.get(i) + 1, ")");
			htmlContent.setSpan(new ForegroundColorSpan(color), startArray.get(i) - 1, endArray.get(i) + 1,
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return htmlContent;
	}

	public static SpannableStringBuilder setImageSpan(String content, Context mContext) throws Exception {
		SpannableStringBuilder mContent = new SpannableStringBuilder(content);
		ArrayList<Integer> startArray = getPosArray2(content, "\\{");
		ArrayList<Integer> endArray = getPosArray2(content, "\\}");
		for (int i = 0; i < startArray.size(); i++) {
			mContent.replace(startArray.get(i) - 1, startArray.get(i), "(");
			mContent.replace(endArray.get(i), endArray.get(i) + 1, ")");
			String compare = mContent.toString().substring(startArray.get(i), endArray.get(i));
			int index = -1;
			for (int j = 0; j < picName.length; j++) {
				if (compare.equals(picName[j])) {
					index = j;
				}
			}
			// 根据资源ID获取资源图像Bitmap对象
			Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), pic[index]);
			// 根据Bitmap对象创建ImageSpan对象
			ImageSpan imageSpan = new ImageSpan(mContext, bitmap);
			mContent.setSpan(imageSpan, startArray.get(i) - 1, endArray.get(i) + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return mContent;
	}

	public static SpannableStringBuilder transString(String content, Context mContext) {
		SpannableStringBuilder mContent = new SpannableStringBuilder(content);
		List<Integer> indexs = new ArrayList<Integer>();
		List<Integer> indexLengh = new ArrayList<Integer>();
		List<Integer> startIndexs = new ArrayList<Integer>();
		for (int i = 0; i < picName.length; i++) {
			if (content.contains(picName[i])) {
				// startIndexs.add(content.indexOf(picName[i]));
				// indexs.add(i);
				// indexLengh.add(picName[i].length());

				String regex = picName[i];
				Pattern pattern = Pattern.compile(regex);
				Matcher matcher = pattern.matcher(content);
				while (matcher.find()) {
					indexs.add(pic[i]);
					startIndexs.add(matcher.start());
					indexLengh.add(picName[i].length());
				}

				for (int j = 0; j < startIndexs.size(); j++) {
					// 根据资源ID获取资源图像Bitmap对象
					Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), indexs.get(j));
					// 根据Bitmap对象创建ImageSpan对象
					ImageSpan imageSpan = new ImageSpan(mContext, bitmap);
					if (indexLengh.get(j) == 2) {
						mContent.setSpan(imageSpan, startIndexs.get(j), startIndexs.get(j) + 2,
								Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					} else {
						mContent.setSpan(imageSpan, startIndexs.get(j), startIndexs.get(j) + 3,
								Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					}
				}

			}
		}
		return mContent;
	}

	/**
	 * 格式化字符串，并返回Spannable 字体变色
	 *
	 * @param start
	 *            开始的索引
	 * @param end
	 *            结束的索引
	 * @param content
	 *            文本
	 * @param color
	 *            字体颜色
	 * @return
	 * @throws Exception
	 */
	public static SpannableStringBuilder colorAdaptableStr(int start, int end, String content, int color)
			throws Exception {
		SpannableStringBuilder htmlContent = new SpannableStringBuilder(content);
		htmlContent.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		return htmlContent;
	}

	/**
	 * 计算出坐标放入集合中。
	 *
	 * @param content
	 * @param regex
	 * @return
	 */
	public static ArrayList<Integer> getPosArray2(String content, String regex) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		ArrayList<Integer> array = new ArrayList<Integer>();
		while (matcher.find()) {
			if (regex.equals("\\{")) {
				array.add(matcher.end());
			} else if (regex.equals("\\}")) {
				array.add(matcher.start());
			}
		}
		return array;
	}
}
